﻿using System;
using System.Web.Mvc;
using Turquoise.Helpers;

namespace Turquoise.Web.Common.Infrastructure
{
    /// <summary>
    /// 
    /// </summary>
    public class TurquoiseRazorViewEngine : RazorViewEngine
    {
        private int _removeInitialNamespaceCount;
        private readonly int _untilClosingNamespaceCount;
        private readonly bool _removeInitialNamespaceAutomatic;

        /// <summary>
        /// 
        /// </summary>
        public TurquoiseRazorViewEngine()
        {
            var defaultTheme = ConfigurationHelper.GetAppSetting("DefaultTheme");
            _removeInitialNamespaceCount = Convert.ToInt32(ConfigurationHelper.GetAppSetting("RemoveInitialNamespaceCount"));
            _untilClosingNamespaceCount = Convert.ToInt32(ConfigurationHelper.GetAppSetting("UntilClosingNamespaceCount"));
            _removeInitialNamespaceAutomatic = Convert.ToBoolean(ConfigurationHelper.GetAppSetting("RemoveInitialNamespaceAutomatic"));
            var locations = new[] {
                "~/Views/%1/{1}/{0}.cshtml",
                "~/Views/Themes/" + defaultTheme + "/%1/{1}/{0}.cshtml"
            };

            MasterLocationFormats = locations;
            ViewLocationFormats = locations;
            PartialViewLocationFormats = locations;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="partialPath"></param>
        /// <returns></returns>
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            var nameSpace = controllerContext.Controller.GetType().Namespace;
            var newPath = partialPath.Replace("%1", ChangePath(nameSpace));
            return base.CreatePartialView(controllerContext, newPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="viewPath"></param>
        /// <param name="masterPath"></param>
        /// <returns></returns>
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            var nameSpace = controllerContext.Controller.GetType().Namespace;
            var newPath = viewPath.Replace("%1", ChangePath(nameSpace));
            var newMasterPath = masterPath.Replace("%1", ChangePath(nameSpace));
            return base.CreateView(controllerContext, newPath, newMasterPath);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="virtualPath"></param>
        /// <returns></returns>
        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            var nameSpace = controllerContext.Controller.GetType().Namespace;
            var newPath = virtualPath.Replace("%1", ChangePath(nameSpace));
            var t = base.FileExists(controllerContext, newPath);
            return t;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pathNamespace"></param>
        /// <returns></returns>
        string ChangePath(string pathNamespace)
        {
            var y = pathNamespace.Replace('.', '/').Split('/');
            var path = string.Empty;
            if (_removeInitialNamespaceAutomatic)
            {
                for (var i = 0; i < y.Length; i++)
                {
                    if (!y[i].Contains("Controllers")) continue;
                    _removeInitialNamespaceCount = i;
                    _removeInitialNamespaceCount++;
                    break;
                }
            }

            for (var i = _removeInitialNamespaceCount; i < y.Length - _untilClosingNamespaceCount; i++)
            {
                if (i == 2)
                {
                    path += y[i];
                }
                else
                {
                    path += "/" + y[i];
                }
            }
            return path;
        }
    }
}
